package com.uls.ldcm.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import com.uls.ldcm.domain.Laborers;
import com.uls.ldcm.domain.Salaryschemes;
import com.uls.ldcm.service.ILaborersService;
import com.uls.ldcm.service.ISalaryschemesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ldcm.domain.Contracts;
import com.uls.ldcm.service.IContractsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合同信息Controller
 * 
 * @author gw
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/ldcm/contracts")
public class ContractsController extends BaseController
{
    @Autowired
    private IContractsService contractsService;

    @Autowired
    private ISalaryschemesService salaryschemesService;



    @Autowired
    private MinioUtils minioUtils;


    @Autowired
    private MinioProp minioProp;

    /**
     * 查询合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contracts contracts)
    {
        startPage();
        List<Contracts> list = contractsService.selectContractsList(contracts);
        for(Contracts contracts1:list){
            Salaryschemes salaryschemes=salaryschemesService.selectSalaryschemesBySalaryschemeId(contracts1.getSalaryschemeId());
            if(salaryschemes!=null) {
                contracts1.setSalaryschemeName(salaryschemes.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:export')")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contracts contracts)
    {
        List<Contracts> list = contractsService.selectContractsList(contracts);
        ExcelUtil<Contracts> util = new ExcelUtil<Contracts>(Contracts.class);
        util.exportExcel(response, list, "合同信息数据");
    }

    /**
     * 获取合同信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:query')")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId)
    {
        return success(contractsService.selectContractsByContractId(contractId));
    }

    /**
     * 新增合同信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:add')")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contracts contracts)
    {
        return toAjax(contractsService.insertContracts(contracts));
    }

    /**
     * 修改合同信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:edit')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contracts contracts)
    {
        return toAjax(contractsService.updateContracts(contracts));
    }

    /**
     * 删除合同信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:contracts:remove')")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(contractsService.deleteContractsByContractIds(contractIds));
    }

    /**
     * 上传合同文件
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }

    @PreAuthorize("@ss.hasPermi('ldcm:contracts:down')")
    @PostMapping("/fileDown")
    public  void imageDown(String fileName, HttpServletResponse response){
        minioUtils.download(minioProp.getBucket(),fileName,response);
    }
}
